home *** CD-ROM | disk | FTP | other *** search
- VERSION 4.00
- Begin VB.Form SubEdgeForm
- Caption = "SubEdge"
- ClientHeight = 4845
- ClientLeft = 1170
- ClientTop = 1215
- ClientWidth = 7110
- Height = 5535
- Left = 1110
- LinkTopic = "Form1"
- ScaleHeight = 323
- ScaleMode = 3 'Pixel
- ScaleWidth = 474
- Top = 585
- Width = 7230
- Begin VB.PictureBox ToPict
- AutoRedraw = -1 'True
- Height = 4815
- Left = 3600
- Picture = "SUBEDGE.frx":0000
- ScaleHeight = 317
- ScaleMode = 3 'Pixel
- ScaleWidth = 229
- TabIndex = 1
- Top = 0
- Width = 3495
- End
- Begin VB.PictureBox FromPict
- AutoRedraw = -1 'True
- Height = 4815
- Left = 0
- ScaleHeight = 317
- ScaleMode = 3 'Pixel
- ScaleWidth = 229
- TabIndex = 0
- Top = 0
- Width = 3495
- End
- Begin MSComDlg.CommonDialog FileDialog
- Left = 3360
- Top = 0
- _Version = 65536
- _ExtentX = 847
- _ExtentY = 847
- _StockProps = 0
- CancelError = -1 'True
- End
- Begin VB.Menu mnuFile
- Caption = "&File"
- Begin VB.Menu mnuFileLoad
- Caption = "&Load..."
- Shortcut = ^L
- End
- Begin VB.Menu mnuFileSep2
- Caption = "-"
- End
- Begin VB.Menu mnuFileExit
- Caption = "E&xit"
- End
- End
- Attribute VB_Name = "SubEdgeForm"
- Attribute VB_Creatable = False
- Attribute VB_Exposed = False
- Option Explicit
- Dim SysPalSize As Integer
- Dim NumStaticColors As Integer
- Dim StaticColor1 As Integer
- Dim StaticColor2 As Integer
- Dim LogPal As Integer
- Dim bytes() As Byte
- Dim wid As Long
- Dim hgt As Long
- Dim palentry(0 To 255) As PALETTEENTRY
- ' ************************************************
- ' Return the index of the nonstatic gray closest
- ' to the given value (assuming the non-static
- ' colors are a gray scale created by
- ' MatchGrayPalette).
- ' ************************************************
- Function NearestNonstaticGray(c As Integer) As Integer
- Dim dgray As Single
- If c < 0 Then
- c = 0
- ElseIf c > 255 Then
- c = 255
- End If
- dgray = 255 / (StaticColor2 - StaticColor1 - 2)
- NearestNonstaticGray = c / dgray + StaticColor1 + 1
- End Function
- ' ***********************************************
- ' Load the indicated file and prepare to work
- ' with its palette.
- ' ***********************************************
- Sub LoadFromPict(fname As String)
- Dim i As Integer
- On Error GoTo LoadFileError
- FromPict.Picture = LoadPicture(fname)
- On Error GoTo 0
- MatchGrayPalette FromPict
- Caption = "SubEdge [" & fname & "]"
- Exit Sub
- LoadFileError:
- Beep
- MsgBox "Error loading file " & fname & "." & _
- vbCrLf & Error$
- Exit Sub
- End Sub
- ' ************************************************
- ' Subtract the image from itself shifted +1 pixel
- ' in the X and Y directions to highlight edges.
- ' ************************************************
- Sub SubtractEdges()
- Dim x As Integer
- Dim y As Integer
- Dim result() As Byte
- Dim status As Long
- Dim c1 As Integer
- Dim c2 As Integer
- ToPict.Picture = FromPict.Image
- ' Subtract the image.
- ReDim result(1 To wid, 1 To hgt)
- For y = 1 To hgt - 1
- For x = 1 To wid - 1
- With palentry(bytes(x + 1, y + 1))
- c1 = (CInt(.peRed) + .peGreen + .peBlue) / 3
- End With
- With palentry(bytes(x, y))
- c2 = (CInt(.peRed) + .peGreen + .peBlue) / 3
- End With
- result(x, y) = _
- NearestNonstaticGray(Abs(c1 - c2))
- Next x
- Next y
- ' Black out edges where there's no subtraction.
- For y = 1 To hgt
- result(wid, y) = StaticColor1 + 1
- Next y
- For x = 1 To wid
- result(x, hgt) = StaticColor1 + 1
- Next x
- ' Display the result.
- status = SetBitmapBits(ToPict.Image, wid * hgt, result(1, 1))
- ToPict.Refresh
- ' Let ToPict rerealize its palette.
- ToPict.ZOrder
- End Sub
- ' ***********************************************
- ' Load the control's palette so it matches the
- ' the system palette. Remap any of the image's
- ' pixels that use static colors to non-static
- ' colors.
- ' Set the following module global variables.
- ' LogPal Image logical palette handle.
- ' palentry() Image logical palette entries.
- ' wid Width of image.
- ' hgt Height of image.
- ' bytes(1 To wid, 1 To hgt)
- ' Image pixel values.
- ' ***********************************************
- Sub MatchColorPalette(pic As Control)
- Dim sys(0 To 255) As PALETTEENTRY
- Dim i As Integer
- Dim bm As BITMAP
- Dim hbm As Integer
- Dim status As Long
- Dim x As Integer
- Dim y As Integer
- Dim clr As Integer
- ' Make sure pic has the foreground palette.
- pic.ZOrder
- i = RealizePalette(pic.hdc)
- DoEvents
- ' Get the system palette entries.
- i = GetSystemPaletteEntries(pic.hdc, 0, SysPalSize, sys(0))
-
- ' Make the logical palette as big as possible.
- LogPal = pic.Picture.hPal
- If ResizePalette(LogPal, SysPalSize) = 0 Then
- Beep
- MsgBox "Error resizing logical palette.", _
- vbExclamation
- Exit Sub
- End If
- ' Blank the non-static colors.
- For i = 0 To StaticColor1
- palentry(i) = sys(i)
- Next i
- For i = StaticColor1 + 1 To StaticColor2 - 1
- With palentry(i)
- .peRed = 0
- .peGreen = 0
- .peBlue = 0
- .peFlags = PC_NOCOLLAPSE
- End With
- Next i
- For i = StaticColor2 To 255
- palentry(i) = sys(i)
- Next i
- i = SetPaletteEntries(LogPal, 0, SysPalSize, palentry(0))
- ' Insert the non-static colors.
- For i = StaticColor1 + 1 To StaticColor2 - 1
- palentry(i) = sys(i)
- palentry(i).peFlags = PC_NOCOLLAPSE
- Next i
- i = SetPaletteEntries(LogPal, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1))
- ' Realize the new palette.
- i = RealizePalette(pic.hdc)
- ' Get the image pixels.
- hbm = pic.Image
- status = GetObject(hbm, BITMAP_SIZE, bm)
- wid = bm.bmWidthBytes
- hgt = bm.bmHeight
- ReDim bytes(1 To wid, 1 To hgt)
- status = GetBitmapBits(hbm, wid * hgt, bytes(1, 1))
- ' Remap any pixels using static colors.
- For y = 1 To hgt
- For x = 1 To wid
- clr = bytes(x, y)
- If clr <= StaticColor1 Or clr >= StaticColor2 Then
- With sys(clr)
- bytes(x, y) = _
- NearestNonstaticColor( _
- .peRed, .peGreen, .peBlue)
- End With
- End If
- Next x
- Next y
- ' Update the image's pixel values.
- status = SetBitmapBits(hbm, wid * hgt, bytes(1, 1))
- pic.Refresh
- End Sub
- ' ***********************************************
- ' Load the control's palette so the non-static
- ' colors are grays. Map the logical palette to
- ' match the system palette. Convert the image to
- ' use the non-static grays.
- ' Set the following module global variables.
- ' LogPal Image logical palette handle.
- ' palentry() Image logical palette entries.
- ' wid Width of image.
- ' hgt Height of image.
- ' bytes(1 To wid, 1 To hgt)
- ' Image pixel values.
- ' ***********************************************
- Sub MatchGrayPalette(pic As Control)
- Dim sys(0 To 255) As PALETTEENTRY
- Dim i As Integer
- Dim bm As BITMAP
- Dim hbm As Integer
- Dim status As Long
- Dim x As Integer
- Dim y As Integer
- Dim gray As Single
- Dim dgray As Single
- Dim c As Integer
- Dim clr As Integer
- ' Make sure pic has the foreground palette.
- pic.ZOrder
- i = RealizePalette(pic.hdc)
- DoEvents
- ' Get the system palette entries.
- i = GetSystemPaletteEntries(pic.hdc, 0, SysPalSize, sys(0))
-
- ' Get the image pixels.
- hbm = pic.Image
- status = GetObject(hbm, BITMAP_SIZE, bm)
- wid = bm.bmWidthBytes
- hgt = bm.bmHeight
- ReDim bytes(1 To wid, 1 To hgt)
- status = GetBitmapBits(hbm, wid * hgt, bytes(1, 1))
- ' Make the logical palette as big as possible.
- LogPal = pic.Picture.hPal
- If ResizePalette(LogPal, SysPalSize) = 0 Then
- Beep
- MsgBox "Error resizing logical palette.", _
- vbExclamation
- Exit Sub
- End If
- ' Blank the non-static colors.
- For i = 0 To StaticColor1
- palentry(i) = sys(i)
- Next i
- For i = StaticColor1 + 1 To StaticColor2 - 1
- With palentry(i)
- .peRed = 0
- .peGreen = 0
- .peBlue = 0
- .peFlags = PC_NOCOLLAPSE
- End With
- Next i
- For i = StaticColor2 To 255
- palentry(i) = sys(i)
- Next i
- i = SetPaletteEntries(LogPal, 0, SysPalSize, palentry(0))
- ' Insert the non-static grays.
- gray = 0
- dgray = 255 / (StaticColor2 - StaticColor1 - 2)
- For i = StaticColor1 + 1 To StaticColor2 - 1
- c = gray
- gray = gray + dgray
- With palentry(i)
- .peRed = c
- .peGreen = c
- .peBlue = c
- End With
- Next i
- i = SetPaletteEntries(LogPal, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1))
- ' Recreate the image using the new colors.
- For y = 1 To hgt
- For x = 1 To wid
- clr = bytes(x, y)
- With sys(clr)
- c = (CInt(.peRed) + .peGreen + .peBlue) / 3
- End With
- bytes(x, y) = NearestNonstaticGray(c)
- Next x
- Next y
- status = SetBitmapBits(hbm, wid * hgt, bytes(1, 1))
- ' Realize the gray palette.
- i = RealizePalette(pic.hdc)
- pic.Refresh
- End Sub
- ' ************************************************
- ' Return the index of the nonstatic color closest
- ' to the given color value.
- ' ************************************************
- Function NearestNonstaticColor(ByVal r As Integer, ByVal g As Integer, ByVal b As Integer) As Integer
- Dim best_i As Integer
- Dim best_dist As Long
- Dim dist As Long
- Dim dr As Long
- Dim dg As Long
- Dim db As Long
- Dim i As Integer
- best_dist = 1000000
- For i = StaticColor1 + 1 To StaticColor2 - 1
- With palentry(i)
- dr = r - .peRed
- dg = g - .peGreen
- db = b - .peBlue
- dist = dr * dr + dg * dg + db * db
- End With
- If best_dist > dist Then
- best_i = i
- best_dist = dist
- End If
- Next i
- NearestNonstaticColor = best_i
- End Function
- ' ***********************************************
- ' Give the form and all the picture boxes an
- ' hourglass cursor.
- ' ***********************************************
- Sub WaitStart()
- MousePointer = vbHourglass
- FromPict.MousePointer = vbHourglass
- ToPict.MousePointer = vbHourglass
- DoEvents
- End Sub
- ' ***********************************************
- ' Restore the mouse pointers for the form and all
- ' the picture boxes.
- ' ***********************************************
- Sub WaitEnd()
- MousePointer = vbDefault
- FromPict.MousePointer = vbDefault
- ToPict.MousePointer = vbDefault
- End Sub
- Private Sub Form_Load()
- ' Make sure the screen supports palettes.
- If Not GetDeviceCaps(hdc, RASTERCAPS) And RC_PALETTE Then
- Beep
- MsgBox "This monitor does not support palettes.", _
- vbCritical
- End
- End If
- ' Get system palette size and # static colors.
- SysPalSize = GetDeviceCaps(hdc, SIZEPALETTE)
- NumStaticColors = GetDeviceCaps(hdc, NUMRESERVED)
- StaticColor1 = NumStaticColors \ 2 - 1
- StaticColor2 = SysPalSize - NumStaticColors \ 2
- ' Make ToPict use grays.
- MatchGrayPalette ToPict
- End Sub
- ' ***********************************************
- ' Make the picture as large as possible.
- ' ***********************************************
- Private Sub Form_Resize()
- Const GAP = 4
- Dim hgt As Single
- Dim wid As Single
- If WindowState = vbMinimized Then Exit Sub
-
- hgt = ScaleHeight
- wid = (ScaleWidth - GAP - 1) / 2
- FromPict.Move 0, 0, wid, hgt
- ToPict.Move FromPict.Width + GAP, 0, wid, hgt
- End Sub
- Private Sub Form_Unload(Cancel As Integer)
- End
- End Sub
- ' ***********************************************
- ' Load a new image file.
- ' ***********************************************
- Private Sub mnuFileLoad_Click()
- Dim fname As String
- ' Allow the user to pick a file.
- On Error Resume Next
- FileDialog.filename = "*.BMP;*.ICO;*.RLE;*.WMF;*.DIB"
- FileDialog.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
- FileDialog.ShowOpen
- If Err.Number = cdlCancel Then
- Exit Sub
- ElseIf Err.Number <> 0 Then
- Beep
- MsgBox "Error selecting file.", , vbExclamation
- Exit Sub
- End If
- On Error GoTo 0
- fname = Trim$(FileDialog.filename)
- FileDialog.InitDir = Left$(fname, Len(fname) _
- - Len(FileDialog.FileTitle) - 1)
- ' Load the picture.
- WaitStart
- DoEvents
- LoadFromPict fname
- SubtractEdges
- WaitEnd
- End Sub
- ' ***********************************************
- ' End the application. (See also the QueryUnload
- ' event.)
- ' ***********************************************
- Private Sub mnuFileExit_Click()
- Unload Me
- End Sub
-